package com.smart.mip.system.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.smart.mip.system.dao.RoleInfoRepository;
import com.smart.mip.system.entity.RoleInfo;
/**
 * RoleInfo  Controller.
 * create by scaffold 2017-07-18 
 * @author codefan@sina.com
 * 角色信息null   
*/


@Controller
@RequestMapping("/system/roleinfo")
public class RoleInfoController{
	
	@Autowired
	private RoleInfoRepository roleInfoRepository;	

    /**
     * 查询所有   角色信息  列表
     *
     * @param field    json中只保存需要的属性名
     * @param request  {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @return {data:[]}
     */
    @RequestMapping(method = RequestMethod.GET)
    public String list(HttpServletRequest request, HttpServletResponse response, Model model) {   
		List<RoleInfo> listObjects = roleInfoRepository.findAll();
        if (!listObjects.isEmpty()) {
			model.addAttribute("LIST", listObjects);
		}
        return "roleInfoList";
    }
    
    /**
     * 查询单个  角色信息 
	
	 * @param roleCode  ROLE_CODE
     * @param catalogCode 主键
     * 
     * @param response    {@link HttpServletResponse}
     * @return {data:{}}
     */
    @RequestMapping(value = "/{roleCode}", method = {RequestMethod.GET})
    public void getRoleInfo(@PathVariable String roleCode, HttpServletResponse response, Model model) {
    	
    	RoleInfo roleInfo =     			
    			roleInfoRepository.findOne( roleCode);
        
        model.addAttribute(roleInfo);
    }
    
    /**
     * 新增 角色信息
     *
     * @param roleInfo  {@link RoleInfo}
     * @return
     */
    @RequestMapping(method = {RequestMethod.POST})
    public String createRoleInfo(@RequestBody @Valid RoleInfo roleInfo, HttpServletResponse response) {
    	roleInfoRepository.save(roleInfo);
        return "redirect:/";
    }

    /**
     * 删除单个  角色信息 
	
	 * @param roleCode  ROLE_CODE
     */
    @RequestMapping(value = "/{roleCode}", method = {RequestMethod.DELETE})
    public String deleteRoleInfo(@PathVariable String roleCode, HttpServletResponse response) {
    	
    	roleInfoRepository.delete( roleCode);
        
       return "redirect:/";
    } 
    
    /**
     * 新增或保存 角色信息 
    
	 * @param roleCode  ROLE_CODE
	 * @param roleInfo  {@link RoleInfo}
     * @param response    {@link HttpServletResponse}
     */
    @RequestMapping(value = "/{roleCode}", method = {RequestMethod.PUT})
    public String updateRoleInfo(@PathVariable String roleCode, 
    	@RequestBody @Valid RoleInfo roleInfo, HttpServletResponse response) {
    	
    	
    	RoleInfo dbRoleInfo  =     			
    			roleInfoRepository.findOne( roleCode);
        
        

        if (null != roleInfo) {
        	dbRoleInfo .copy(roleInfo);
        	roleInfoRepository.saveAndFlush(dbRoleInfo);
        } 

        return "redirect:/";
    }
}
